package cn.lcfms.utils;

public class StringUtils {
	/**
	 * 将字符串转换为数字
	 * @param data 字符串
	 * @param df 如果为空设置默认
	 * @return
	 */
	public static int StringToInteger(String data,int df){
		if(null==data){
			return df;
		}else{
			try {
				return Integer.valueOf(data);
			} catch (Exception e) {
				return df;
			}
			
		}		
	}
	public static float StringToFloat(String data,float df){
		if(null==data){
			return df;
		}else{
			try {
				return Float.valueOf(data);
			} catch (Exception e) {
				return df;
			}
			
		}		
	}
	public static long StringToLong(String data,long df){
		if(null==data){
			return df;
		}else{
			try {
				return Long.valueOf(data);
			} catch (Exception e) {
				return df;
			}
			
		}		
	}
	/**
	 * 分割字符串数组
	 * @param strs 字符串数组
	 * @param sp 分隔符
	 * @return
	 */
	public static String join(String[] strs,String sp) {
		if(null==strs || strs.length==0) {
			return "";
		}
		StringBuilder r=new StringBuilder();
		for(int i=0;i<strs.length;i++) {
			r.append(strs[i]);
			if(i!=strs.length-1) {
				r.append(sp);
			}
		}
		return r.toString();
	}
	public static String join(int[] strs,String sp) {	
		if(null==strs || strs.length==0) {
			return "";
		}
		StringBuilder r=new StringBuilder();
		for(int i=0;i<strs.length;i++) {
			r.append(strs[i]);
			if(i!=strs.length-1) {
				r.append(sp);
			}
		}
		return r.toString();
	}
	public static String join(float[] strs,String sp) {
		if(null==strs || strs.length==0) {
			return "";
		}
		StringBuilder r=new StringBuilder();
		for(int i=0;i<strs.length;i++) {
			r.append(strs[i]);
			if(i!=strs.length-1) {
				r.append(sp);
			}
		}
		return r.toString();
	}
	public static String join(long[] strs,String sp) {
		if(null==strs || strs.length==0) {
			return "";
		}
		StringBuilder r=new StringBuilder();
		for(int i=0;i<strs.length;i++) {
			r.append(strs[i]);
			if(i!=strs.length-1) {
				r.append(sp);
			}
		}
		return r.toString();
	}
	
	public static int[] splitInt(String str,String split) {
		String[] s = str.split(split);
		int[] i=new int[s.length];
		for(int j=0;j<s.length;j++) {
			i[j]=StringToInteger(s[j], 0);
		}
		return i;
	}
	
	public static float[] splitFloat(String str,String split) {
		String[] s = str.split(split);
		float[] i=new float[s.length];
		for(int j=0;j<s.length;j++) {
			i[j]=StringToFloat(s[j], 0);
		}
		return i;
	}
	
	public static long[] splitLong(String str,String split) {
		String[] s = str.split(split);
		long[] i=new long[s.length];
		for(int j=0;j<s.length;j++) {
			i[j]=StringToLong(s[j], 0);
		}
		return i;
	}
	
}
